﻿using CRMG.Container.Resources.ExceptionResources;
using CRMG.Container.Source.Helper;
using CRMG.Container.Source.OpManager;
using CRMG.Container.Source.Variables;
using CRMG.Container.Views.BaseList;
using CRMG.UserManagement;
using Guru.ErrorHandling;
using Guru.Framework.Util;
using System;
using System.Collections;
using System.Data;
using System.Resources;

namespace CRMG.Container.Views.InterViews
{
    public partial class FrmInterViewList : FrmBaseList
    {
        InterViewManager intVwMan;
        StudentManager stdntMan;
        UserManager userMan;
        public FrmInterViewList()
            : base()
        {
            try
            {
                this.InitializeComponent();
                this.MaximumSize = this.Size;
                this.MinimumSize = this.Size;
                intVwMan = new InterViewManager();
                stdntMan = new StudentManager();
                userMan = new UserManager();
                formRes = new ResourceManager("CRMG.Container.Resources.ControlResources.WinFormStrings", typeof(FrmInterViewList).Assembly);
                messageRes = new ResourceManager("CRMG.Container.Resources.MessageResources.AppMessages", typeof(FrmInterViewList).Assembly);

                dtMain = intVwMan.GetInterViews();
                dtSearch = dtMain;
                ReGenerateGrid();
                this.ColumnHeaderList = formRes.GetString("FrmInterViewList.ColumnHeaderList");
                this.Text = formRes.GetString("FrmInterViewList.Caption");
                SetResources();
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmInterViewList.Ctor.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2064,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2064)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterViewList.Ctor.Error"),
                    messageRes.GetString("Error"));
            }
        }

        protected override void FormLoad(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = stdntMan.GetActiveStudentList();
                DataRow dr = dt.NewRow();
                dr["Name"] = formRes.GetString("FrmBaseList.AllStudents");
                dr["OBJID"] = 0;
                dt.Rows.Add(dr);
                ControlUtil.LoadDataTable2DropDownList(drpStudent, dt, "Name", "OBJID");
                dt.Dispose();
                dt = new DataTable();
                dt = userMan.GetUserList();
                dr = dt.NewRow();
                dr["Name"] = formRes.GetString("FrmBaseList.AllUsers");
                dr["OBJID"] = 0;
                dt.Rows.Add(dr);
                ControlUtil.LoadDataTable2DropDownList(drpUser, dt, "Name", "OBJID");
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                       messageRes.GetString("FrmInterViewList.Load.Error"),
                       messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2065,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2065)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterViewList.Load.Error"),
                    messageRes.GetString("Error"));
            }
        }

        protected override void GoToPageClick(object sender, EventArgs e)
        {
            try
            {
                base.GoToPageClick(sender, e);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2066,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2066)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterViewList.GoToPage.Error"),
                    messageRes.GetString("Error"));
            }
        }

        protected override void SetPage(int page)
        {
            try
            {
                base.SetPage(page);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2067,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2067)
                };
                logger.Write();
            }
        }

        protected override void Next(object sender, EventArgs e)
        {
            SetPage(currentPage + 1);
        }

        protected override void Previous(object sender, EventArgs e)
        {
            SetPage(currentPage - 1);
        }

        protected override void First(object sender, EventArgs e)
        {
            SetPage(0);
        }

        protected override void Last(object sender, EventArgs e)
        {
            SetPage(totalPage);
        }

        protected override void Search(object sender, EventArgs e)
        {
            try
            {
                dtSearch = dtMain;

                // User Id
                int Id = drpUser.SelectedValue.ToInt();
                if (Id != 0)
                {
                    dtSearch = DataUtil.FilterDataTableWithOneColumn(dtMain, "UserId", Id);
                }
                Id = 0;
                // Student Id
                Id = drpStudent.SelectedValue.ToInt();
                if (Id != 0)
                {
                    dtSearch = DataUtil.FilterDataTableWithOneColumn(dtMain, "StudentId", Id);
                }

                if (drpSearchType.Items.Count == 4)
                {
                    switch (drpSearchType.SelectedIndex)
                    {
                        case 0:
                            dtSearch = DataUtil.FilterDataTableWithDateLaterOrBefore(dtSearch, "InterViewTime", dtpStart.Value, true);
                            break;
                        case 1:
                            dtSearch = DataUtil.FilterDataTableWithDateBetween(dtSearch, "InterViewTime", dtpStart.Value, dtpEnd.Value);
                            break;
                        case 2:
                            dtSearch = DataUtil.FilterDataTableWithDateLaterOrBefore(dtSearch, "InterViewTime", dtpEnd.Value, false);
                            break;
                        default:
                            break;
                    }
                }

                ReGenerateGrid();
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2068,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2068)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("FrmInterViewList.Search.Error"),
                    messageRes.GetString("Error"));
            }
        }

        protected override void RefreshRecords(object sender, EventArgs e)
        {
            try
            {
                dtMain = intVwMan.GetInterViews();
                dtSearch = dtMain;
                ReGenerateGrid();
            }
            catch (GuruException)
            {
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("RefreshRecords.Error"),
                    messageRes.GetString("Error"));
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2085,
                    Message = exc.Message,
                    StackTrace = exc.StackTrace,
                    Title = Rc.GetString(2085)
                };
                logger.Write();
                ControlUtil.ShowErrorMessage(
                    messageRes.GetString("RefreshRecords.Error"),
                    messageRes.GetString("Error"));
            }
        }

        protected override void grdListCellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
        {
            if (grdList.SelectedRows.Count < 1)
            {
                ControlUtil.ShowWarningMessage(
                    formRes.GetString("FrmInterviewList.grdList.NoRow"),
                    formRes.GetString("Warning"));

                return;
            }

            Hashtable h = new Hashtable();
            h.Add(grdList.Columns["UserFullName"].HeaderText, grdList.SelectedRows[0].Cells["UserFullName"].Value.ToStr());
            h.Add(grdList.Columns["StudentFullName"].HeaderText, grdList.SelectedRows[0].Cells["StudentFullName"].Value.ToStr());
            h.Add(grdList.Columns["InterViewTime"].HeaderText, grdList.SelectedRows[0].Cells["InterViewTime"].Value.ToStr());
            h.Add(grdList.Columns["Remarks"].HeaderText, grdList.SelectedRows[0].Cells["Remarks"].Value.ToStr());
            h.Add(grdList.Columns["Comments"].HeaderText, grdList.SelectedRows[0].Cells["Comments"].Value.ToStr());
            FrmMessage frmMes = new FrmMessage(formRes.GetString("FrmMessage.InterView"), h);
            frmMes.ShowDialog();
        }
    }
}
